﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.LotterySrv;
using SLLottery.Common;

namespace SLLottery
{
    public partial class ChgPwd : ChildWindow
    {
        private User user = null;
        private List<BankInfo> obsBankInfoList;
        private LotterySrv.LotteryServiceClient srv;
        public ChgPwd()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.GetBankInfoCompleted += new EventHandler<GetBankInfoCompletedEventArgs>(srv_GetBankInfoCompleted);
            srv.ValidateLoginCompleted += new EventHandler<ValidateLoginCompletedEventArgs>(srv_ValidateLoginCompleted);
            srv.ValidateBankPasswordCompleted += new EventHandler<ValidateBankPasswordCompletedEventArgs>(srv_ValidateBankPasswordCompleted);
            srv.ModifyPasswordCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(srv_ModifyPasswordCompleted);
            srv.ModifyBankPwdCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(srv_ModifyBankPwdCompleted);
            srv.SetBankInfoCompleted += new EventHandler<SetBankInfoCompletedEventArgs>(srv_SetBankInfoCompleted);
            rbtChgPwd.IsChecked = true;
            user = App.Session[Constant.USERINFO_SESSION] as User;
            BindBankInfo();
        }

        void srv_SetBankInfoCompleted(object sender, SetBankInfoCompletedEventArgs e)
        {
            if (e.Result)
            {
                gridLoading.Visibility = Visibility.Collapsed;
                this.Close();
            }
            else
            {
                gridLoading.Visibility = Visibility.Collapsed;
                MessageBox.Show("注意：一个银行账号不能绑定多个账户!");
            }
        }

        void srv_ModifyBankPwdCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            Messager.Show("资金密码修改成功！");
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void rbtChgPwd_Checked(object sender, RoutedEventArgs e)
        {
            this.btOk.Visibility = Visibility.Visible;
            gridPassword.Visibility = Visibility.Visible;
            gridBankInfoSet.Visibility = Visibility.Collapsed;
            gridBankInfoShow.Visibility = Visibility.Collapsed;
            gridBankInfo.Visibility = Visibility.Collapsed;
            lbPasswordName.Text = "登录密码";
            this.lbErrorMsg.Text = "";
            tbPassword0.Password = "";
            tbPassword1.Password = "";
            tbPassword2.Password = "";
        }

        private void rbtChgBankPwd_Checked(object sender, RoutedEventArgs e)
        {
            this.btOk.Visibility = Visibility.Visible;
            gridPassword.Visibility = Visibility.Visible;
            gridBankInfoSet.Visibility = Visibility.Collapsed;
            gridBankInfoShow.Visibility = Visibility.Collapsed;
            gridBankInfo.Visibility = Visibility.Collapsed;
            lbPasswordName.Text = "资金密码";
            this.lbErrorMsg.Text = "";
            tbPassword0.Password = "";
            tbPassword1.Password = "";
            tbPassword2.Password = "";
            if (obsBankInfoList.Count == 0)
            {
                lbErrorMsg.Text = "尚未设置银行资料，无法修改资金密码!";
                this.btOk.Visibility = Visibility.Collapsed;
            }
        }

        private void rbtBank_Checked(object sender, RoutedEventArgs e)
        {
            gridPassword.Visibility = Visibility.Collapsed;
            gridBankInfo.Visibility = Visibility.Visible;
            if (obsBankInfoList.Count == 0)
            {
                this.btOk.Visibility = Visibility.Visible;
                gridBankInfoSet.Visibility = Visibility.Visible;
                gridBankInfoShow.Visibility = Visibility.Collapsed;
            }
            else
            {
                gridBankInfoShow.Visibility = Visibility.Visible;
                gridBankInfoSet.Visibility = Visibility.Collapsed;
                BindBankDetail(obsBankInfoList[0].BankType);
                if (obsBankInfoList[0].BankType == "财付通")
                    cbxBankType.SelectedIndex = 1;
                else
                    cbxBankType.SelectedIndex = 0;
            }
            this.lbErrorMsg.Text = "";
            tbBankAccount.Text = "";
            tbAccountName.Text = "";
            tbBankPassword0.Password = "";
            tbBankPassword1.Password = "";
        }

        private void btOk_Click(object sender, RoutedEventArgs e)
        {
            this.lbErrorMsg.Text = "";
            if (rbtChgPwd.IsChecked.Value)
            {
                if (string.IsNullOrWhiteSpace(tbPassword0.Password.Trim()) || string.IsNullOrWhiteSpace(tbPassword1.Password.Trim()))
                {
                    this.lbErrorMsg.Text = "密码不能为空";
                    return;
                }
                if (!Common.Common.ValidateInput(tbPassword0.Password))
                {
                    this.lbErrorMsg.Text = "密码只接受数字、英文、下划线";
                    return;
                }
                if (!Common.Common.ValidateInput(tbPassword1.Password))
                {
                    this.lbErrorMsg.Text = "密码只接受数字、英文、下划线";
                    return;
                }
                if (tbPassword1.Password.Length < 6)
                {
                    this.lbErrorMsg.Text = "密码太短(6位以上)";
                    return;
                }
                if (tbPassword1.Password != tbPassword2.Password.Trim())
                {
                    this.lbErrorMsg.Text = "两次输入的密码不一致";
                    return;
                }
                gridLoading.Visibility = Visibility.Visible;
                srv.ValidateLoginAsync(user.UserId, tbPassword0.Password.Trim());

            }
            else if (rbtChgBankPwd.IsChecked.Value)
            {
                if (string.IsNullOrWhiteSpace(tbPassword0.Password.Trim()) || string.IsNullOrWhiteSpace(tbPassword1.Password.Trim()))
                {
                    this.lbErrorMsg.Text = "密码不能为空";
                    return;
                }
                if (!Common.Common.ValidateInput(tbPassword0.Password))
                {
                    this.lbErrorMsg.Text = "密码只接受数字、英文、下划线";
                    return;
                }
                if (!Common.Common.ValidateInput(tbPassword1.Password))
                {
                    this.lbErrorMsg.Text = "密码只接受数字、英文、下划线";
                    return;
                }
                if (tbPassword1.Password.Length < 6)
                {
                    this.lbErrorMsg.Text = "密码太短(6位以上)";
                    return;
                }
                if (tbPassword1.Password != tbPassword2.Password)
                {
                    this.lbErrorMsg.Text = "两次输入的密码不一致";
                    return;
                }
                gridLoading.Visibility = Visibility.Visible;
                srv.ValidateBankPasswordAsync(user.UserId, tbPassword0.Password.Trim());
            }
            else if (this.rbtBank.IsChecked.Value)
            {
                if (obsBankInfoList.Count >= 2)
                {
                    return;
                }
                if (string.IsNullOrWhiteSpace(tbBankAccount.Text.Trim()) || (tbBankAccount.Text.Trim().Length > 30))
                {
                    this.lbErrorMsg.Text = "银行账号输入有误(30位以内)";
                    return;
                }
                if (!Common.Common.ValidateNums(tbBankAccount.Text))
                {
                    this.lbErrorMsg.Text = "银行账号只接受数字(不要输入空格)";
                    return;
                }
                if (string.IsNullOrWhiteSpace(tbAccountName.Text.Trim()) || (tbAccountName.Text.Trim().Length > 20))
                {
                    this.lbErrorMsg.Text = "银行户名输入有误(20位以内)";
                    return;
                }
                if (!Common.Common.ValidateBankName(tbAccountName.Text))
                {
                    this.lbErrorMsg.Text = "银行户名只接受数字、中文、英文";
                    return;
                }
                if (obsBankInfoList.Count == 0)
                {
                    if (string.IsNullOrWhiteSpace(tbBankPassword0.Password.Trim()) || (tbBankPassword0.Password.Trim().Length < 6))
                    {
                        this.lbErrorMsg.Text = "资金密码输入有误(6位以上)";
                        return;
                    }
                    if (!Common.Common.ValidateInput(tbBankPassword0.Password))
                    {
                        this.lbErrorMsg.Text = "密码只接受数字、英文、下划线";
                        return;
                    }
                    if (tbBankPassword0.Password.Trim() != tbBankPassword1.Password.Trim())
                    {
                        this.lbErrorMsg.Text = "两次输入的密码不一致";
                        return;
                    }
                }
                gridLoading.Visibility = Visibility.Visible;
                if (obsBankInfoList.Count == 1)
                {
                    srv.SetBankInfoAsync(user.UserId, (cbxBankType.SelectedValue as System.Windows.Controls.ComboBoxItem).Content.ToString(), tbAccountName.Text.Trim(), obsBankInfoList[0].BankPwd, tbBankAccount.Text.Trim());
                }
                else
                {
                    srv.SetBankInfoAsync(user.UserId, (cbxBankType.SelectedValue as System.Windows.Controls.ComboBoxItem).Content.ToString(), tbAccountName.Text.Trim(), tbBankPassword1.Password.Trim(), tbBankAccount.Text.Trim());
                }
            }
        }

        void srv_ValidateLoginCompleted(object sender, ValidateLoginCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result)
            {
                srv.ModifyPasswordAsync(user.UserId, tbPassword1.Password.Trim());
            }
            else
            {
                Messager.Show("登录密码错误，请重新输入！");
            }
        }
        void srv_ValidateBankPasswordCompleted(object sender, ValidateBankPasswordCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result)
            {
                srv.ModifyBankPwdAsync(user.UserId, tbPassword1.Password.Trim());
            }
            else
            {
                Messager.Show("资金密码错误，请重新输入！");
            }
        }

        private void BindBankInfo()
        {
            srv.GetBankInfoAsync(user.UserId);
        }
        void srv_GetBankInfoCompleted(object sender, GetBankInfoCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            if (e.Result != null || e.Result.Count > 0)
            {
                obsBankInfoList = e.Result.ToList();
                rbtBank.IsEnabled = true;
                rbtChgBankPwd.IsEnabled = true;
            }
            else
            {
                lbErrorMsg.Text = "尚未设置银行资料,无法修改资金密码！";
            }
        }
        private void BindBankDetail(string BankType)
        {
            if (obsBankInfoList.Count == 0)
            {
                gridBankInfoSet.Visibility = Visibility.Visible;
                gridBankInfoShow.Visibility = Visibility.Collapsed;
                this.btOk.Visibility = Visibility.Visible;
            }
            else if (obsBankInfoList.Count == 1 && obsBankInfoList[0].BankType != BankType)
            {
                gridBankInfoSet.Visibility = Visibility.Visible;
                gridBankInfoShow.Visibility = Visibility.Collapsed;
                tbBankPwd.Visibility = Visibility.Collapsed;
                tbBankPassword0.Visibility = Visibility.Collapsed;
                tbBankConfirmPwd.Visibility = Visibility.Collapsed;
                tbBankPassword1.Visibility = Visibility.Collapsed;
                this.btOk.Visibility = Visibility.Visible;
            }
            else
            {
                BankInfo bankInfo = obsBankInfoList.Where(p => p.BankType == BankType).First();
                gridBankInfoShow.Visibility = Visibility.Visible;
                gridBankInfoSet.Visibility = Visibility.Collapsed;
                this.btOk.Visibility = Visibility.Collapsed;
                if (bankInfo.CardNo.Length >= 4)
                {
                    if (BankType == "财付通")
                        lbBankAccount.Text = "*****" + bankInfo.CardNo.Substring(bankInfo.CardNo.Length - 4, 4);
                    else
                        lbBankAccount.Text = "**************" + bankInfo.CardNo.Substring(bankInfo.CardNo.Length - 4, 4);
                }
                else
                    lbBankAccount.Text = bankInfo.CardNo;
                lbAccountName.Text = bankInfo.UserName.Substring(0, 1) + "**";
            }
        }
        void srv_ModifyPasswordCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            gridLoading.Visibility = Visibility.Collapsed;
            Messager.Show("登录密码修改成功！");
        }

        private void cbxBankType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lbErrorMsg != null)
                this.lbErrorMsg.Text = string.Empty;
            if (cbxBankType != null)
            {
                BindBankDetail((cbxBankType.SelectedValue as System.Windows.Controls.ComboBoxItem).Content.ToString());
            }
        }
    }
}

